package com.cxwx.mybatiesplus.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxwx.mybatiesplus.dao.BookMapper;
import com.cxwx.mybatiesplus.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    BookMapper bookMapper;

    @Override
    public List<Book> searchBooks(Book book) {
        LambdaQueryWrapper<Book> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (book.getAuthor() != null && book.getAuthor() != "") {
            lambdaQueryWrapper.like(Book::getAuthor, book.getAuthor());
        }
        if (book.getName() != null && book.getName() != "") {
            lambdaQueryWrapper.like(Book::getName, book.getName());
        }
        if (book.getCategory() != null && book.getCategory() != "") {
            lambdaQueryWrapper.like(Book::getCategory, book.getCategory());
        }

        return bookMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage getPage(Integer pageNum) {
        IPage page = new Page(pageNum, 3);
        return bookMapper.selectPage(page, null);

    }
}
